import {NextRequest, NextResponse} from "next/server";
import {PrismaClient} from "@prisma/client"
interface User {
    userId: string
}
// 创建prisma客户端对象实例
const prisma = new PrismaClient()

export async function POST(request: NextRequest) {

    try {
        const {userId} = await request.json() as User

        if (!userId) {
            return NextResponse.json({message: "Bad Request, Invalid userId"}, {status: 400})
        }
        // 根据id查找用户信息是否存在
        let user = await prisma.user.findUnique({
            where: {
                userId
            }
        })
        if (!user) {
             user = await prisma.user.create({
                data: {
                    userId
                }
            })
        }
        return NextResponse.json({user}, {status: 200})
    }catch (e) {
        console.log("file:route.ts:16 ~ POST ~ e", e)
        return NextResponse.json({
            message: "Internal Error"
        }, {status: 500})
    }finally {
        // 关闭连接
        await prisma.$connect()
    }
}